﻿using System;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class SupplierGroupBiz
    {
        #region Properties

        private readonly ISupplierGroupDao _supplierGroupDao;

        #endregion

        #region Constructors

        public SupplierGroupBiz()
        {
            _supplierGroupDao = new SupplierGroupDao();
        }

        public SupplierGroupBiz(ISupplierGroupDao supplierGroupDao)
        {
            _supplierGroupDao = supplierGroupDao;
        }

        #endregion

        #region Methods

        public SupplierGroup GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _supplierGroupDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public SupplierGroup CreateGroup(SupplierGroup supplierGroup)
        {
            // validate inputs
            if (supplierGroup == null) return null;
            if (string.IsNullOrEmpty(supplierGroup.Code) || string.IsNullOrEmpty(supplierGroup.Name)) return null;

            try
            {
                return _supplierGroupDao.AddSupplierGroup(supplierGroup);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _supplierGroupDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _supplierGroupDao.GetCurrentIdentity() + 1;
        }

        public void UpdateGroup(SupplierGroup supplierGroup)
        {
            if (supplierGroup == null) return;
            try
            {
                _supplierGroupDao.UpdateSupplierGroup(supplierGroup);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
        }

        public object GetAllSupplierGroupsWs()
        {
            try
            {
                var peopleList = _supplierGroupDao.GetAllSupplierGroups();
                return new { Result = "OK", Records = peopleList };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateGroupWs(SupplierGroup supplierGroup)
        {
            // validate inputs
            if (supplierGroup == null) return null;
            if (string.IsNullOrEmpty(supplierGroup.Code) || string.IsNullOrEmpty(supplierGroup.Name)) return null;

            try
            {
                var addedSupplierGroup = _supplierGroupDao.AddSupplierGroup(supplierGroup);
                return new { Result = "OK", Record = addedSupplierGroup };
            }
            catch (Exception ex)
            {
                //throw;
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateGroupWs(SupplierGroup supplierGroup)
        {
            if (supplierGroup == null) return null;
            try
            {
                _supplierGroupDao.UpdateSupplierGroup(supplierGroup);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteGroupWs(int supplierGroupId)
        {
            try
            {
                _supplierGroupDao.DeleteSupplierGroup(supplierGroupId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion

        #region Helpers

        #endregion
    }
}
